package com.wonders.module.demo.controller;

import com.wonders.framework.entity.vo.Page;
import com.wonders.framework.util.StringUtil;
import com.wonders.framework.util.web.RequestUtil;
import com.wonders.framework.web.BaseController;
import com.wonders.module.demo.entity.vo.DemoVo;
import com.wonders.module.demo.service.DemoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/demo")
public class DemoController extends BaseController {

	@Autowired
	private DemoService demoService;

	@RequestMapping("toQuery")
	public Object toQuery() {
		return "demo/list";
	}

	@RequestMapping("query")
	@ResponseBody
	public Object query() {
		DemoVo demoVo = (DemoVo) RequestUtil.requestParamToBean(DemoVo.class);
		Page page = demoService.query(demoVo, super.getDataGridPage());
		return super.dataGridResult(page);
	}

	@RequestMapping("toForm")
	public Object toForm() {
		return "demo/form";
	}

	@RequestMapping("save")
	@ResponseBody
	public Object save() {
		DemoVo demoVo = (DemoVo) RequestUtil.requestParamToBean(DemoVo.class);
		String action = (String) RequestUtil.getRequestParam("action");
		if ("edit".equals(action))
			demoService.update(demoVo);
		else if ("add".equals(action))
			demoService.save(demoVo);
		else {
			return super.ajaxError("缺少参数");
		}
		return super.ajaxSuccess();
	}

	@RequestMapping("load")
	@ResponseBody
	public Object load() {
		DemoVo demoVo = (DemoVo) RequestUtil.requestParamToBean(DemoVo.class);
		if (demoVo.getId() == null) {
			return super.ajaxError("缺少参数");
		}
		demoVo = demoService.load(demoVo.getId());
		return super.ajaxSuccess(demoVo);
	}

	@RequestMapping("remove")
	@ResponseBody
	public Object remove() {
		String keys = (String) RequestUtil.getRequestParam("keys");
		if (StringUtils.isEmpty(keys)) {
			return super.ajaxError("缺少参数");
		}
		demoService.remove(StringUtil.stringToList(keys, Long.class));
		return super.ajaxSuccess();
	}
}